/**
 * 定义站点地图模块
 */
(function(window, angular, undefined){
    var module = angular.module('water.apps.sw.bcParamMap212.bcParamMap212Manager', ['water.entity.bcParamMap212',
        'water.entity.protocol']);

    module.controller('bcParamMap212ManagerController',function($scope,$bcParamMap212Service,
                                                                  $bcMonitorParamDictService,
                                                                  $protocolService){

        $scope.protocols;

        /**
         * 设备列表数据
         */
        $scope.paramList;

        /**
         * 是否显示列表
         */
        $scope.isShowList = true;
        /**
         * 是否显示新建页面
         */
        $scope.isShowAdd = false;
        /**
         * 是否显示编辑页面
         */
        $scope.isShowEdit = false;

        /**
         * 用于新建的model
         */
        $scope.addModel;
        /**
         * 用于编辑的model
         */
        $scope.editModel;

        /**
         * 打开新增窗口按钮点击事件
         */
        $scope.onOpenCreateClick = function(){
            $scope.addModel = {};
            $scope.showAdd();
        }

        $scope.getProtocolList = function () {
            $protocolService.getList().then(function (data) {
                var protocols = [];
                for (var i = 0; i < data.length; i++) {
                    var item = data[i];
                    protocols.push({
                        k:item.id,
                        v:item.name
                    });
                }
                $scope.protocols = protocols;
                $scope.ProtocolModel.setData(protocols);
                $scope.editProtocolModel.setData(protocols);
            });
        };


        /**
         * 获取传输列表的数据,使传输列表的ID和名称与05映射库表的名称关联
         */
        $scope.getDeviceData = function () {
            $bcMonitorParamDictService.getList().then(function (data) {
                var paramData = [];

                for (var i = 0; i < data.length; i++) {
                    paramData.push({
                        k: data[i].ID,
                        v: data[i].PARAM_CODE_BUS
                    });
                }
                $scope.paramData = paramData;
                $scope.paramDataModel.setData(paramData);
                $scope.editParamDataModel.setData(paramData);
            });
        };

        /**
         * 保存按钮点击事件
         */
        $scope.onSaveClick = function(model){

            for(var i=0;i<$scope.paramData.length;i++){
                if(model.PARAM_CODE_BUS==$scope.paramData[i].k){
                    model.PARAM_CODE_BUS = $scope.paramData[i].v;
                }
            }

            for(var i=0;i<$scope.protocols.length;i++){
                if(model.TRANSPORT_PROTOCOL_ID==$scope.protocols[i].k){
                    model.TRANSPORT_PROTOCOL_NAME = $scope.protocols[i].v;
                }
            }

            $bcParamMap212Service.create(model).then(function(){
                $scope.getList();
                $scope.showList();
                alert("保存成功");
                $scope._init();
            }).catch(function(error){
                alert(error);
            });
        }

        /**
         * 打开编辑窗口按钮点击事件
         */
        $scope.onOpenEditClick = function(model){
            $scope.editModel = model;
            $scope.showEdit();
        }

        /**
         * 删除按钮点击事件
         */
        $scope.onDeleteClick = function(id){
            $bcParamMap212Service.deleteModel(id).then(function(){
                $scope.getList();
                alert("删除成功");
                $scope._init();
            });
        }

        /**
         * 更新按钮点击事件
         */
        $scope.onUpdateClick = function(model){
            for(var i=0;i<$scope.protocols.length;i++){
                if(model.TRANSPORT_PROTOCOL_ID==$scope.protocols[i].k){
                    model.TRANSPORT_PROTOCOL_NAME = $scope.protocols[i].v;
                }
            }
            $bcParamMap212Service.update(model).then(function(){
                $scope.getList();
                $scope.showList();
                alert("保存成功");
                $scope._init();
            }).catch(function(error){
                alert(error);
            });
        }

        /**
         * 只显示列表
         */
        $scope.showList = function(){
            $scope.isShowList = true;
            $scope.isShowAdd = false;
            $scope.isShowEdit = false;
        }

        /**
         * 只显示新建页
         */
        $scope.showAdd = function(){
            $scope.isShowList = false;
            $scope.isShowAdd = true;
        }

        /**
         * 关闭新建页
         */
        $scope.closeAdd = function(){
            $scope.isShowAdd = false;
            $scope.isShowList = true;
        }

        /**
         * 只显示编辑页
         */
        $scope.showEdit = function(){
            $scope.isShowList = false;
            $scope.isShowEdit = true;
        }

        /**
         * 关闭编辑页
         */
        $scope.closeEdit = function(){
            $scope.isShowEdit = false;
            $scope.isShowList = true;
        }

        $scope.getList = function(){
            $bcParamMap212Service.getList().then(function(data){
                $scope.paramList = data;
            });
        }

        $scope._init = function(){
            $scope.getList();
            $scope.showList();
            $scope.getProtocolList();
        }

        $scope._init();
        $scope.getDeviceData();

    });

}(window, angular));


